package com.roc.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.roc.bean.SwitchBean;
import com.roc.dao.IBaseDao;
import com.roc.dao.ISQLBaseDao;
import com.roc.service.ISwitchsService;
import com.roc.util.Constance;
@Service
@Transactional
public class SwitchsServiceImpl implements ISwitchsService {
	static Logger logger=Logger.getLogger(SwitchsServiceImpl.class);
	@Autowired
	private ISQLBaseDao sqlBaseDao;
	@Override
	public SwitchBean findMcthCdSwitch(SwitchBean bean) throws Exception {
		SwitchBean switchbean=null;
		String sql="SELECT T.INS_CD,T.MCHT_CD,T.TRANS_CD,T.BEGIN_TIME,T.END_TIME,T.USE_FLAG FROM  APSTFR.TBL_MGM_RISK_TIME T WHERE T.TRANS_CD=? AND T.INS_CD=? AND T.MCHT_CD=?";
		Object[] parmas=new Object[3];
		parmas[0]=bean.getTransCd();
		parmas[1]=bean.getInsCd();
		parmas[2]=bean.getMchtCd();
		Object object=sqlBaseDao.findObj(sql, parmas);
		if(object!=null)
		{
			Object[] obj=(Object[]) object;
			switchbean=new SwitchBean();
			switchbean.setInsCd(Constance.Null2EmptyTrim(obj[0]));
			switchbean.setMchtCd(Constance.Null2EmptyTrim(obj[1]));
			switchbean.setTransCd(Constance.Null2EmptyTrim(obj[2]));
			switchbean.setBegin(Constance.Null2EmptyTrim(obj[3]));
			switchbean.setEnd(Constance.Null2EmptyTrim(obj[4]));
			switchbean.setFlag(Constance.Null2EmptyTrim(obj[5]));
		}
		return switchbean;
	}

	@Override
	public int updateMcthSwitch(SwitchBean bean) throws Exception {
		String sql="UPDATE APSTFR.TBL_MGM_RISK_TIME SET BEGIN_TIME=?,END_TIME=?,USE_FLAG=? WHERE TRANS_CD=? AND INS_CD=? AND MCHT_CD=?";
		List<Object> parmas=new ArrayList<Object>();
		parmas.add(bean.getBegin());
		parmas.add(bean.getEnd());
		parmas.add(bean.getFlag());
		parmas.add(bean.getTransCd());
		parmas.add(bean.getInsCd());
		parmas.add(bean.getMchtCd());
		int count=sqlBaseDao.deleteBySql(sql, parmas);
		if(count>0)
		{
			logger.info("更改商户【"+bean.getMchtCd()+"】D0交易控制成功!");
		}
		return count;
	}

	@Override
	public SwitchBean findMasterSwitch(SwitchBean bean) throws Exception {
		SwitchBean switchbean=null;
		String sql="SELECT T.TRN_CD,T.TRN_NAME,T.BEGIN_TIME,T.END_TIME,T.USE_FLAG FROM APSTFR.TBL_MGM_POS_CTRL_TIME T WHERE T.TRN_CD='1171'";
		Object object=sqlBaseDao.findObj(sql, null);
		if(object!=null)
		{
			Object[] obj=(Object[])object;
			switchbean=new SwitchBean();
			switchbean.setTranCd(Constance.Null2EmptyTrim(obj[0]));
			switchbean.setTranName(Constance.Null2EmptyTrim(obj[1]));
			switchbean.setBegin(Constance.Null2EmptyTrim(obj[2]));
			switchbean.setEnd(Constance.Null2EmptyTrim(obj[3]));
			switchbean.setFlag(Constance.Null2EmptyTrim(obj[4]));
		}
		return switchbean;
	}

	@Override
	public int updateMasterSwitch(SwitchBean bean) throws Exception {
		String sql="UPDATE APSTFR.TBL_MGM_POS_CTRL_TIME SET BEGIN_TIME=?,END_TIME=?,USE_FLAG=? WHERE TRN_CD=?";
		List<Object> parmas=new ArrayList<Object>();
		parmas.add(bean.getBegin());
		parmas.add(bean.getEnd());
		parmas.add(bean.getFlag());
		parmas.add(bean.getTranCd());
		int count=sqlBaseDao.deleteBySql(sql, parmas);
		if(count>0)
		{
			logger.info("更改D0总交易控制成功!");
		}
		return count;
	}

	@Override
	public Map<String,Object> addMcthSwitch(SwitchBean bean) throws Exception {
		Map<String,Object> resultMap=new HashMap<String, Object>();
		SwitchBean isExsit=findMcthCdSwitch(bean);
		String status=null;
		if(isExsit!=null){
			status="此条商户交易控制记录已存在!";
			logger.info(status);
			resultMap.put("result", status);
			return resultMap;
		}
		String InsertSql="INSERT INTO APSTFR.TBL_MGM_RISK_TIME(INS_CD,MCHT_CD,TRANS_CD,BEGIN_TIME,END_TIME,USE_FLAG) VALUES(?,?,?,?,?,?)";
		List<Object> parmas=new ArrayList<Object>();
		parmas.add(bean.getInsCd());
		parmas.add(bean.getMchtCd());
		parmas.add(bean.getTransCd());
		parmas.add(bean.getBegin());
		parmas.add(bean.getEnd());
		parmas.add(bean.getFlag());
		
		int count=sqlBaseDao.deleteBySql(InsertSql, parmas);
		if(count>0)
		{
			logger.info("更改D0总交易控制成功!");
		}
		return resultMap;
	}

}
